function isNumber(n) {
	return !isNaN(parseFloat(n)) && isFinite(n);
}

function getDuration(earlier_date, later_date)
{
	if(earlier_date == "YYYY-MM-DD" || later_date == "YYYY-MM-DD")
		return false;

	var later_year = later_date.substr( 0, 4);
	var later_month = later_date.substr( 5, 2);
	var later_day = later_date.substr( 8, 2);
	
	var earlier_year = earlier_date.substr( 0, 4);
	var earlier_month = earlier_date.substr( 5, 2);
	var earlier_day = earlier_date.substr( 8, 2);
	
	var later_culm = (later_year * 365) + (later_month * 12) + (later_day);
	var earlier_culm = (earlier_year * 365) + (earlier_month * 12) + (earlier_day);
	
	if(earlier_culm > later_culm)
		return 0;
	else 
		return later_culm - earlier_culm + 1;
}

/* If the date currently reads YYYY-MM-DD, wipe the date. 
 * Otherwise, leave the date as it is. 
 * 
 */
function clickDate(obj) {
	//el = element
	var el = document.getElementById(obj);

	//alert("Clicked Date!");
	el.style.color = "black";
	
	if(el.value == "YYYY-MM-DD"){
		el.value = "";
	}	
}

/* Check the dates and return true if the second date
 * is greater than the first. 
 *
 * 
 */
function compareDates(earlier_dateobj, later_dateobj, ed_feedback, ld_feedback) 
{
	//alert ("GOT HERE");
	var ed_obj = document.getElementById(earlier_dateobj); //earlier date object
	var ld_obj = document.getElementById(later_dateobj); //later date object

	var edf_obj = document.getElementById(ed_feedback); //earlier date object
	var ldf_obj = document.getElementById(ld_feedback); //later date object

	//alert(ld_obj.value);
	if(ed_obj.value == "YYYY-MM-DD" || ld_obj.value == "YYYY-MM-DD")
	{
		//alert ("NOT DATE FORMATS");
		return false;
	}
	
	//alert (ed_obj.value+"  "+ ld_obj.value);
	if( getDuration(ed_obj.value, ld_obj.value) > 0)
	{
		//alert ("DURATION GREATER THAN ZERO!");
		//ldf_obj.style.color = "grey";
		return true;
	}
	else
	{
		//alert ("DURATION ZERO!" + ldf_obj.value);
		ldf_obj.style.color = "#FF0000";
		ldf_obj.firstChild.data = "Something went wrong - is the End date before the Start date?";
		return false;
	}
}

/* If the user clicks off the date and the text box has 
 * YYYY-MM-DD, set the box to blank. Otherwise, leave the 
 * box with the entered values. 
 */
function restoreDate(obj) {
	//el = element
	var el = document.getElementById(obj);

	if(el.value == ""){
		el.value = "YYYY-MM-DD";
		el.style.color = "grey";
	}	
}

/*Rigorously checks the date field to make sure it is properly formatted.
 * 
 * Takes a date input field ID and a span's ID.
 * When errors are found, it will change the color and text in the span
 * to reflect the error in that box. 
 */
function validateDate(dateinput, example){

	var currentTime = new Date();
	var curryear = currentTime.getFullYear();
	
	var dateinput_el = document.getElementById(dateinput);
	var example_el = document.getElementById(example);

	var day = dateinput_el.value.substr(8,2);
	var month = dateinput_el.value.substr(5,2);
	var year = dateinput_el.value.substr(0,4);
	//alert(month + " " + day + " " + year );
	
	//alert(dateinput_el.value.substr(4,1));
	if(dateinput_el.value == "YYYY-MM-DD" || dateinput_el.value == "")//if nothing or YYYY-MM-DD entered
	{
		example_el.firstChild.data = "ex: 2011-06-24";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(dateinput_el.value.substr(4,1) != '-')//if string length not 10 digits
	{
		example_el.firstChild.data = "First hyphen is missing: YYYY-MM-DD";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(dateinput_el.value.substr(7,1) != '-')//if string length not 10 digits
	{
		example_el.firstChild.data = "Second hyphen is missing: YYYY-MM-DD";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(dateinput_el.value.length != 10)//if string length not 10 digits
	{
		example_el.firstChild.data = "The date format should be ten digits: YYYY-MM-DD";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(!isNumber(year))//if year not 4 digits
	{
		example_el.firstChild.data = "Please provide a 4 digit year: YYYY-MM-DD";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(!isNumber(month))//if month not 2 digits
	{
		example_el.firstChild.data = "Please provide a 2 digit month: YYYY-MM-DD";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(!isNumber(day))//if month not 2 digits
	{
		example_el.firstChild.data = "Please provide a 2 digit day: YYYY-MM-DD";
		dateinput_el.style.border="1px solid red";
	}else if(year < curryear)//if year is in the past
	{
		example_el.firstChild.data = "You can't specify a camp in the past!";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(year > curryear + 5)//if year too far into the future
	{
		example_el.firstChild.data = "You can't specify a camp more than 5 years ahead of time!";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(month > 12)//if month > 12
	{
		example_el.firstChild.data = "Please specify a month (01 to 12)";
		example_el.style.color = "red";
		dateinput_el.style.border="1px solid red";
	}else if(month == 2 && day > 28)//if February and date > 28
	{
		
		//check if it is a leap-year
		var leap_year = 0;
		
		if(year % 400 == 0)
			leap_year = 1;
		else if(year % 100 == 0)
			leap_year = 0;
		else if(year % 4 == 0)
			leap_year = 1;
		else
			leap_year = 0;

		if(leap_year && day == 29)
		{
			example_el.firstChild.data = "";
			example_el.style.color = "green";
			dateinput_el.style.border="1px solid green";
		}else{
			example_el.firstChild.data = "Only "+(28 + leap_year)+" days in February in "+year;
			example_el.style.color = "red";
			dateinput_el.style.border="1px solid red";
		}
	}else if(day > 30)
	{
		//alert("day " + day);
		if( day > 31 || day < 1)
		{
			example_el.firstChild.data = "Months can never have "+day+" days in them!";
			example_el.style.color = "red";
			dateinput_el.style.border="1px solid red";
		}else if( (month == 4 || month == 6 || month == 9 || month == 11) && day == 31)
		{
			example_el.firstChild.data = "Month "+month+" doesn't have 31 days in it";
			example_el.style.color = "red";
			dateinput_el.style.border="1px solid red";
		}else
		{
			example_el.firstChild.data = "";
			example_el.style.color = "green";
			dateinput_el.style.border="1px solid green";
		}
	
	}else//otherwise we're okay!
	{
		example_el.firstChild.data = "";
		example_el.style.color = "green";
		dateinput_el.style.border="1px solid green";
	}
}

/*  Validate one age field
 * 
 * Check					Warning
 * Ages are numeric		Please enter only whole numbers
 * Ages are negative	Please enter only positive whole numbers
 * 
 */
function validateAge(age_obj, age_span_obj)
{
	var age_el = document.getElementById(age_obj);
	var age_span_el = document.getElementById(age_span_obj);
	
	if(!isNumber(age_el.value))
	{
		age_span_el.firstChild.data = "The maximum age isn't a number!";
		age_span_el.style.color = "red";
		age_el.style.border = "1px solid red";
		return true;
	}else if(age_el.value < 0)
	{
		age_span_el.firstChild.data = "The minimum age can't be negative!";
		age_span_el.style.color = "red";
		age_el.style.border = "1px solid red";
		return false;
	}else{
		age_span_el.firstChild.data = "Thanks";
		age_span_el.style.color = "green";
		age_el.style.border = "1px solid green";
		return true;
	}
}
/*  Validates two fields against each other. 
 * 
 * Check					Warning
 * Ages are numeric		Please enter only whole numbers
 * Ages are negative	Please enter only positive whole numbers
 * Age#2 >= Age#1		Max Age must be greater than or equal to Min Age
 * 
 */
function validateAges(age1obj, age2obj, age1spanobj, age2spanobj)
{
	var age1el = document.getElementById(age1obj);
	var age2el = document.getElementById(age2obj);
	
	var age1spanel = document.getElementById(age1spanobj);
	var age2spanel = document.getElementById(age2spanobj);
	//alert(age1el.value+" "+age2el.value+" "+age1spanel.firstChild.data);//+" "+age2spanel.FirstChild.data+" ");
	
	//if both fields are okay on their own
	if(validateAge(age1obj, age1spanobj) && validateAge(age2obj, age2spanobj))
	{
		//if the minage is greater than the maxage
		if( age1el.value > age2el.value )
		{
			age2spanel.firstChild.data = "The Maximum age must be greater than or equal to the minimum age.";
			age2spanel.style.color = "red";
			age2el.style.border = "1px solid red";
		}else
		{//everything is fine, print some thank you messages
			age1spanel.firstChild.data = "Thanks";
			age1spanel.style.color = "green";
			age1el.style.border = "1px solid green";
			age2spanel.firstChild.data = "Thanks";
			age2spanel.style.color = "green";
			age2el.style.border = "1px solid green";
		}
		
	}else
	{
		//they're not okay on their own, let the individual 
		//validateAge() functions print messages
		return false;
	}
	
}



